package com.pcxm.cm.action;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pcxm.cm.pojo.SysUser;

public class SysUserAction extends ActionSupport{
	private SysUser sysUser;
	private String jsonStr;
	private static final Log log = LogFactory.getLog(SysMenuAction.class);
	
	/**
	 * 登录
	 * @return
	 */
	public String login() {
		if (null == sysUser || StringUtils.isBlank(sysUser.getUserName())
					|| StringUtils.isBlank(sysUser.getPassword())) {
			sysUser = null;
		}
		SysUser user=sysUser;
		if (null != sysUser) {
			HttpSession session = (HttpSession) ServletActionContext.getRequest().getSession();
			session.setAttribute("sysuser",user);
			this.setJsonStr("{\"success\":\"true\"}");
		}else {
			this.setJsonStr("{\"success\":\"false\"}");
		}
		return SUCCESS;
	}
	
	/**
	 * 
	 * 注销
	 * @return
	 */
	public String exitLogin() {
		
		Map<String, Object> ctx = ActionContext.getContext().getSession();
		ctx.remove("sysuser");
		return SUCCESS;
	}
	
	/**
	 * 进入主页链接(检测当前session)
	 * @return
	 */
	public String mainPage(){
		Map<String, Object> ctx = ActionContext.getContext().getSession();
		SysUser user=(SysUser) ctx.get("sysuser");
		
		if(null!=user){
			return SUCCESS;
		}
		return "toLogin";
	}

	public SysUser getSysUser() {
		return sysUser;
	}

	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}
	
	
	
}
